package com.muli.muwai.bao;

import com.muli.muwai.api.HttpClientUtil;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class suiBianQiMing {
    //授权常量
    private static String appID = "FSAID_1319a08";

    private static String appSecret = "1af4ddc99f104020963bc07ac521e4e9";

    private static String permanentCode = "A157353D79BB911D9AE9A1A2668FBC00";

    private static String urlTest = "https://open.fxiaoke.com/cgi/corpAccessToken/get/V2";

    private static String urlTest2 = "https://open.fxiaoke.com/cgi/crm/v2/data/query";

    private static String openUserId = "FSUID_42315E8605D5B50901B4366EAF2E5918";

    static HttpClientUtil httpClientUtil = new HttpClientUtil();

    //线索对象接口(客户跟踪表)TraceTable
    public List<TraceTable> customerTracking(String phone) {
        Map map = fenXiangInterface();//17921-16000

        String param = "  {\n" +
                "    \"corpAccessToken\": \"" + map.get("corpAccessToken") + "\",\n" +
                "    \"corpId\": \"" + map.get("corpId") + "\",\n" +
                "    \"currentOpenUserId\": \"" + openUserId + "\",\n" +
                "    \"data\": {\n" +
                "        \"dataObjectApiName\": \"LeadsObj\",\n" +
                "        \"search_query_info\": {\n" +
                "            \"limit\": 200,\n" +
                "            \"offset\": 0,\n" +
                "            \"filters\": [\n" +
                "                {\n" +
                "                    \"field_name\": \"tel\",\n" +
                "                    \"field_values\": [\n" +
                "                        \"" + phone + "\"\n" +
                "                    ],\n" +
                "                    \"operator\": \"IN\"\n" +
                "                }\n" +
                "            ],\n" +
                "        }\n" +
                "    }\n" +
                "  }";
        Map map1 = httpClientUtil.sendPostForJson("", param, urlTest2);
        //System.out.println(map1);
        //获取最终数据
        Map<String, Object> data = (Map<String, Object>) map1.get("data");
        ArrayList<Object> dataList = (ArrayList<Object>) data.get("dataList");
        //定义实体类集合
        List<TraceTable> targetList = new ArrayList();
        System.out.println(targetList);

        //遍历最终数据
        for (int i = 0; i < dataList.size(); i++) {
            TraceTable traceTable = new TraceTable();
            Map<String, Object> mapdata = (Map<String, Object>) dataList.get(i);
            traceTable.setUName((String) mapdata.get("name"));
            traceTable.setUPhone((String) mapdata.get("tel"));
            traceTable.setProjectAddress((String) mapdata.get("field_6Xn0N__c"));
            Object owner = mapdata.get("ShopExcel");//服务人员
            if (owner != null) {
                String s = String.valueOf(owner);
                traceTable.setHead(s);
            } else {
                traceTable.setHead(null);
            }
            traceTable.setLifeStatus((String) mapdata.get("life_status"));//生命状态
            traceTable.setBAmount((String) mapdata.get("field_l0F22__c"));//签约金额
            traceTable.setField18cemC((String) mapdata.get("field_18cem__c"));//省
            traceTable.setFieldfUV5cC((String) mapdata.get("field_fUV5c__c"));//市
            traceTable.setField7IT2aC((String) mapdata.get("field_7IT2a__c"));//区
            traceTable.setOChannel(String.valueOf(mapdata.get("field_6oqSk__c")));//获知渠道
            traceTable.setOwnerDepartment((String) mapdata.get("owner_department"));//门店
            Object create_time = mapdata.get("create_time");
            if (create_time != null) {
                Timestamp timestamp1 = new Timestamp(new Double((Double) create_time).longValue());
                traceTable.setCreationTime(timestamp1);
            } else {
                traceTable.setCreationTime(null);
            }
            targetList.add(traceTable);
        }
        return targetList;
    }

    //获取负责人信息
    public List<IntfPrincipal> owner(String ownerr) {
        Map map = fenXiangInterface();
        String param = " {\n" +
                "    \"corpAccessToken\": \""+map.get("corpAccessToken")+"\",\n" +
                "    \"corpId\": \""+map.get("corpId")+"\",\n" +
                "    \"currentOpenUserId\": \""+openUserId+"\",\n" +
                "    \"data\": {\n" +
                "    \"search_query_info\": {\n" +
                "      \"offset\": 0.0,\n" +
                "      \"limit\": 20,\n" +
                "      \"filters\": [\n" +
                "           {\n" +
                "                    \"field_name\": \"ShopExcel\",\n" +
                "                    \"field_values\": [\n" +
                "                        \""+ownerr+"\"\n" +
                "                    ],\n" +
                "                    \"operator\": \"IN\"\n" +
                "                }\n" +
                "      ]\n" +
                "    },\n" +
                "    \"dataObjectApiName\": \"PersonnelObj\"\n" +
                "  }\n" +
                "  }";
        Map map1 = httpClientUtil.sendPostForJson("", param, urlTest2);
        //获取最终数据
        Map<String, Object> data = (Map<String, Object>) map1.get("data");
        ArrayList<Object> dataList = (ArrayList<Object>) data.get("dataList");
        //定义实体类集合
        List<IntfPrincipal> list = new ArrayList();
        //遍历最终数据
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, Object> mapdata = (Map<String, Object>) dataList.get(i);
            IntfPrincipal entity = new IntfPrincipal();
            entity.setName((String) mapdata.get("name"));
            entity.setLifeStatus(String.valueOf(mapdata.get("life_status")));
            list.add(entity);
        }
        return list;
    }

    //应用级授权阶方法
    public Map fenXiangInterface() {
        String param = "{\n" +
                "    \"appId\": \"" + appID + "\",\n" +
                "    \"appSecret\":\"" + appSecret + "\",\n" +
                "    \"permanentCode\":\"" + permanentCode + "\"\n" +
                "}";
        Map map = httpClientUtil.sendPostForJson("", param, urlTest);
        System.out.println(map);
        return map;

    }
    //测试
    public static void main(String[] args) {
        suiBianQiMing aaa = new suiBianQiMing();
        String s="FSUID_31AA78FB9DF408E8217AFA45C822DDF2";
       List<IntfPrincipal> owner = aaa.owner(s);
        System.out.println("21346");
        System.out.println("21346");
        System.out.println("21346");
       //Map map = aaa.fenXiangInterface();
    }


}